//
//  HPDetailViewController.m
//  Mall
//
//  Created by 孙飞燕 on 16/10/10.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "HPDetailViewController.h"
#import "GoodsDetailViewController.h"
#import "HPFourthCollectionViewCell.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
#import "HPDetailCollectionReusableView.h"
#import "HPDetailCollectionViewCell.h"


@interface HPDetailViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,HPDetailCollectionResuableViewDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong)NSMutableArray *listArr;//品牌数组
@property (nonatomic,assign)NSInteger selectBtn;//点击不同的button
@property (nonatomic,strong)NSMutableArray *dataSource; //collectionView上的数据源
@end

@implementation HPDetailViewController
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (NSMutableArray *)listArr{
    if (!_listArr) {
        _listArr = [NSMutableArray arrayWithObjects:@"鱼跃",@"海尔",@"飞利浦",@"欧姆龙",@"氧生活",@"氧精灵",@"神鹿医疗",@"康居人",nil];
    }
    return _listArr;
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:_flowLayout];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        //注册单元格
        [_collectionView registerNib:[UINib nibWithNibName:@"HPFourthCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HPFourthCollectionViewCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"HPDetailCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HPDetailCollectionViewCell"];
        //注册头部视图
        [self.collectionView registerClass:[HPDetailCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionViewHeader"];

    }
    return _collectionView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.collectionView];
    // Do any additional setup after loading the view.
}
#pragma mark --- 点击头视图的三个按钮的代理方法
- (void)hpDetailCollectionResuableViewDelegate:(HPDetailCollectionReusableView *)hpResuableView andFlag:(int)flag{

    _selectBtn = flag;

    [self.collectionView reloadData];


}
#pragma mark ---- UICollectionView的代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 8;
    }else{
        return 4;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        HPDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HPDetailCollectionViewCell" forIndexPath:indexPath];
        cell.listLabel.text = self.listArr[indexPath.row];
        return cell;
    }else{

        HPFourthCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HPFourthCollectionViewCell" forIndexPath:indexPath];
        switch (_selectBtn) {
            case 12:
                cell.goodsImage.image = [UIImage imageNamed:@"k0"];
                break;
            case 13:
                cell.goodsImage.image = [UIImage imageNamed:@"k1"];
                break;
            default:
                break;
        }
        return cell;
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        if (indexPath.section == 0) {
            HPDetailCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionViewHeader" forIndexPath:indexPath];
            headerView.backgroundColor = [UIColor whiteColor];
            headerView.delegate = self;
            [headerView addSubview:headerView.headView];
            return headerView;
        }else{
            return nil;
        }
    }else{
        return nil;
    }
}
//点击cell进入商品信息详情界面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        GoodsDetailViewController *detailVC = [[GoodsDetailViewController alloc]init];
        //进入详情界面时隐藏底部tabBar
        detailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detailVC animated:YES];

    }
    
}
//item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(kWidth/4, 50);
    }else{
    return CGSizeMake(kWidth/2, 200);
    }
}
//头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(kWidth, 50);
    }else{
        return CGSizeMake(0, 0);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
